package com.ruoyi.config;

/**
 * @author hlf
 * @createTime 2025/2/7 8:45
 * @description
 */

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@AllArgsConstructor
@Configuration
@ConditionalOnProperty(name = "spring.rabbitmq.enabled", havingValue = "true", matchIfMissing = false)
public class MqConfig {

    private final RabbitTemplate rabbitTemplate;



    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                /**
                 * 启动main方法，并通过springapplication.run启动
                 * 应用创建并通过springApplication对象，推断应用类型，设置初始化器，设置自动监听器，确定主应用类
                 * 准备环境configurationEnvironment,加载配置文件，系统环境变量，命令行等参数
                 * 创建并刷新springContext上下文，创建应用上下文，加载配置类和自动配置类，进行依赖注入，初始化bean等操作
                 * 再刷新上下文时启动嵌入式服务器，注册servlet、filter
                 * 对应监听stated事件会被触发
                 * 应用启动完成后，执行commandLineRunner和applicationRunner接口初始化逻辑
                 * 触发ready事件，applicationReadyEvent 事件，应用进入运行状态，处理业务请求或任务
                 **/
                log.error("触发return callback,");
                log.debug("exchange: {}", returned.getExchange());
                log.debug("routingKey: {}", returned.getRoutingKey());
                log.debug("message: {}", returned.getMessage());
                log.debug("replyCode: {}", returned.getReplyCode());
                log.debug("replyText: {}", returned.getReplyText());
            }
        });
    }
}